package service

import (
	log "common/log4go"
	"encoding/json"
	"fmt"
	"iot-bridge/iot_cmd_up/distribute/dao/redis"
	"iot-bridge/iot_cmd_up/distribute/model"
	"time"
)

const (
	iotuptime_redis_key = "iot_uptime_cid_%s"
)

func SaveIotUpTime(cid string) error {
	timestamp := time.Now().Unix()
	info := &model.IotUpTime{
		Cid:      cid,
		LastTime: timestamp,
	}
	bts, err := json.Marshal(info)
	if err != nil {
		log.Error("json.Marshal(%v) error(%v)", info, err)
		return err
	}

	key := fmt.Sprintf(iotuptime_redis_key, cid)
	err = redis.RedisSet(key, string(bts), 0)
	if err != nil {
		log.Error("redis.RedisSet(%s,%s,%d)", key, string(bts), 0)
		return err
	}
	return nil
}
